package nl.weeaboo.sh.level;

import java.awt.Point;
import java.awt.Rectangle;

public class ActiveTiles {

	private Tile tiles[][];
	private Rectangle bounds;
	private Rectangle cameraBounds;
	
	public ActiveTiles(int w, int h) {
		cameraBounds = new Rectangle();
		bounds = new Rectangle(0, 0, w, h);
		tiles = new Tile[w][h];
	}
	
	//Functions
	public void updateTiles(Level level, int cx, int cy) {
		bounds.x = cx - bounds.width / 2;
		bounds.y = cy - bounds.height / 2;
		
		for (int y = bounds.y; y < bounds.y + bounds.height; y++) {
			for (int x = bounds.x; x < bounds.x + bounds.width; x++) {
				Room r = level.getRoom(x, y);
				if (r != null) {
					tiles[x-bounds.x][y-bounds.y] = r.getTile(new Point(x-r.getX(), y-r.getY()));
				} else {
					tiles[x-bounds.x][y-bounds.y] = null;
				}
			}
		}
		
		cameraBounds.setBounds(-1000000, -1000000, 2000000, 2000000);
		Point p = new Point(cx, cy);
		if (level.getCamera().isPlayerTarget()) {
			p.x = Level.toTileCoord(level.getPlayer().getX());
			p.y = Level.toTileCoord(level.getPlayer().getY());
		}
		Room r = level.getRoom(p.x, p.y);
		if (r != null) cameraBounds.setBounds(r.getCameraBounds());
	}
	
	//Getters	
	public Rectangle getCameraBounds() { return cameraBounds; }
	public Rectangle getBounds() { return bounds; }
	public int getX() { return bounds.x; }
	public int getY() { return bounds.y; }
	public int getWidth() { return bounds.width; }
	public int getHeight() { return bounds.height; }
	public Tile[][] getTiles() { return tiles; }
	public Tile getTile(int x, int y) {
		return (x >= 0 && x < bounds.width && y >= 0 && y < bounds.height ? tiles[x][y] : null);
	}
	
	//Setters
	
}
